﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using School.Common;

namespace School.Pages.Admission
{
    public partial class DeleteStudent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1026) == true)
                {
                    if (!IsPostBack)
                    {
                        LstCampuses.DataSource = Structure.GetCampuses();
                        LstCampuses.DataTextField = "Name";
                        LstCampuses.DataValueField = "ID";
                        LstCampuses.DataBind();

                        LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
                        LstCertificates.DataValueField = "Id";
                        LstCertificates.DataTextField = "Name";
                        LstCertificates.DataBind();

                        /*ListBox1.DataSource = Expense.GetAllExpenses();
                        ListBox1.DataValueField = "ID";
                        ListBox1.DataTextField = "Name";
                        ListBox1.DataBind();*/

                        LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });
                        LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
                        LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });
                        lstClasses.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الفصل" });
                        lstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        protected void LstCampuses_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
            LstCertificates.DataValueField = "Id";
            LstCertificates.DataTextField = "Name";
            LstCertificates.DataBind();

            LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });
        }

        protected void LstCertificates_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstStages.DataSource = Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
            LstStages.DataValueField = "Id";
            LstStages.DataTextField = "Name";
            LstStages.DataBind();
            LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
        }

        protected void LstStages_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
            LstGrades.DataValueField = "Id";
            LstGrades.DataTextField = "Name";
            LstGrades.DataBind();
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });
        }

        protected void LstGrades_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstClasses.DataSource = Structure.GetClassesByGrade(long.Parse(LstGrades.SelectedValue));
            lstClasses.DataValueField = "Id";
            lstClasses.DataTextField = "Name";
            lstClasses.DataBind();
            lstClasses.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الفصل" });

            lstStudents.DataSource = Students.GetStudentsByGrade(long.Parse(LstGrades.SelectedValue)).Select(x => new { x.Id, Name = x.Ar_FirstName + " " + x.Ar_SecondName + " " + x.Ar_ThirdNmae + " " + x.Ar_FourthName });
            lstStudents.DataValueField = "Id";
            lstStudents.DataTextField = "Name";
            lstStudents.DataBind();
            lstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });
        }

        protected void lstClasses_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstStudents.DataSource = Students.GetStudentsByClass(int.Parse(lstClasses.SelectedValue)).Select(x => new { x.Id, Name = x.Ar_FirstName + " " + x.Ar_SecondName + " " + x.Ar_ThirdNmae + " " + x.Ar_FourthName });
            lstStudents.DataValueField = "Id";
            lstStudents.DataTextField = "Name";
            lstStudents.DataBind();
            lstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });
        }

        protected void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
        {
            HFStudentId.Value = lstStudents.SelectedValue;
            lblResult.Text = "";
        }

        protected void HFStudentId_ValueChanged(object sender, EventArgs e)
        {

        }
        
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                var db = new SchoolEntities();
                var id = long.Parse(HFStudentId.Value);

                var voucher = from x in db.ACC_Voucher where x.Student_Id == id select x.Expense_Id;
                
                if (voucher.Count() > 0)
                {
                    lblResult.ForeColor = System.Drawing.Color.Red;
                    lblResult.Text = "يوجد فاتورة لهذا الطالب ولا يمكن حذفه";
                    return;
                }

                var student = db.TE_Student.Single(x => x.Id == id);

                try { db.DeleteObject(student.SD_Father); db.SaveChanges(); } catch { }
                try { db.DeleteObject(student.SD_Mother); db.SaveChanges(); } catch { }
                try { db.DeleteObject(student.ACC_Payment_Order); db.SaveChanges(); } catch { }
                try { db.DeleteObject(student); db.SaveChanges(); } catch { }
                lblResult.ForeColor = System.Drawing.Color.Green;
                lblResult.Text = "تم الحذف";
            }
            catch
            {
                lblResult.ForeColor = System.Drawing.Color.Red;
                lblResult.Text = "حدث خطأ فى عملية الحذف";
            }
        }

        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            long id = 0;
            string name = null;
            
            long.TryParse(TxtStudentNumber.Text, out id);
            name = TxtStudentName.Text;

            var d = Students.Search(id, name);

            if (d.Count() > 0)
            {
                StudentsGrid.DataSource = d;
                StudentsGrid.DataBind();
                StudentsPanel.Visible = true;
                StudentsGrid.Visible = true;
                LblSearchNotFound.Visible = false;
            }
            else
            {
                StudentsPanel.Visible = true;
                LblSearchNotFound.Visible = true;
                StudentsGrid.Visible = false;
            }

            TxtStudentName.Text = "";
            TxtStudentNumber.Text = "";
            lblResult.Text = "";

        }

        protected void StudentsGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            HFStudentId.Value = ((Label)StudentsGrid.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("LblStudentId")).Text;
            StudentsPanel.Visible = false;
        }
    }
}